package com.jrzh.mvc.controller.sys.admin;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jrzh.common.tools.CollectionTools;
import com.jrzh.framework.annotation.RequiredPermission;
import com.jrzh.framework.base.controller.BaseAdminController;
import com.jrzh.framework.base.search.BaseSearch;
import com.jrzh.framework.bean.JqDataGrid;
import com.jrzh.mvc.constants.SysActionConstants;
import com.jrzh.mvc.model.sys.RoleModel;
import com.jrzh.mvc.model.sys.UserModel;
import com.jrzh.mvc.search.sys.UserEventSearch;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.UserEventView;

@RestController(UserEventController.LOCATION +"/UserEventController")
@RequestMapping(UserEventController.LOCATION)
public class UserEventController extends BaseAdminController{
	public static final String LOCATION = "sys/admin/userEvent";
	
	@Autowired
	SysServiceManage sysServiceManage;
	
	@PostMapping(value = "datagrid")
	@RequiredPermission(key =  SysActionConstants.INDEX)
	public JqDataGrid<UserEventView> datagrid(UserEventSearch search) {
		JqDataGrid<UserEventView> dg = new JqDataGrid<UserEventView>();
		try {
			search.setOrder(BaseSearch.Order_Type_Desc);
			dg =  sysServiceManage.userEventService.datagrid(search);
			for(UserEventView userEventView : dg.getRows()){
	    		UserModel user=sysServiceManage.userService.findById(userEventView.getUserId());
	    		if(null!=user){
	    			List<RoleModel> roles=user.getRoles();
	    			if(CollectionTools.isNotBlank(roles)){
	    				String[] rolesName = new String[roles.size()];
	    				for (int i = 0; i < rolesName.length; i++) {
	    					rolesName[i] = roles.get(i).getName();
	    				}
	    				userEventView.setRoles(StringUtils.join(rolesName,","));
	    			}
	    		}
	    	}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dg;
	}

	@Override
	protected void setData() {
		
	}
}
